﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MINERP.NewPBAuto.IView;
using MINERP.BLL.NewPB;
using System.Threading;
using MINERP.MODEL.FoundationModel;
using System.Windows.Forms;

namespace MINERP.NewPBAuto.Presenter
{
    class FAtuoPBPresenter
    {
        IFAutoPBView _view;
        NewPBBLL _bll;

        Thread thAutoMakeup = null;

        int SusBoardNum = 0;
        int TotalPNum = 0;

        public FAtuoPBPresenter(IFAutoPBView view)
        {
            _view = view;
            _bll = new NewPBBLL();
            _bll.AutoPBResultEvent += new GetFAutoPBResult(_bll_AutoPBResultEvent);
            _bll.StopPBEvent += new NewPBBLL.StopPBDelete(_bll_StopPBEvent);
        }

        void _bll_StopPBEvent()//停止拼板
        {
            _view.SetBtn("开始",true);
        }

        void _bll_AutoPBResultEvent(int result, float wrate, int Pnum, int overPnum)//每拼板完成就自动更新界面
        {
            string strResult = "失败";
            string strStatus = "...";
            string strCurBStatus = "";

            switch (result)
            {
                case 0: SusBoardNum++;
                    TotalPNum += overPnum;
                    strStatus = "成功拼板";
                    //strResult = "已拼:  " + SusBoardNum.ToString() + " 块大板    当前拼的浪费率：" + (wrate * 100).ToString("0.00") + "%    已拼照片数： " + TotalPNum.ToString() + " 剩余照片数： " + Pnum.ToString();
                    //strResult = "已拼:  " + SusBoardNum.ToString() + " 块大板    当前拼的浪费率：" + (wrate * 100).ToString("0.00") + "%    已拼照片数： " + TotalPNum.ToString();
                    strCurBStatus = "当前拼的浪费率：" + (wrate * 100).ToString("0.00") + "%  照片数：" + overPnum.ToString();
                    _view.UpCtrlStatus(strCurBStatus);
                    break;
                case 1: strStatus = "图片已全部拼完";
                    break;
                case 2: strStatus = "自动拼板程序无法拼剩余的图片，请手动拼板!";
                    break;
                case 3: strStatus = "在向数据库添加数据时出错，自动拼板将停止!";
                    break;
                case 4: strStatus = "正在检查拼板结果...";
                    break;
                case 5:
                    strStatus = "正在收集数据...";
                    break;
                case 6:
                    strStatus = "正在进行自动拼板...";
                    break;
                case 7:
                    strStatus = "正在检查拼板结果,   当前浪费率：" + (wrate * 100).ToString("0.00") + "%";
                    break;
                case 8://显示清柜的照片数量
                    _view.UpCtrlClearBoxCount("还剩清柜照片："+overPnum.ToString()+" 张");
                    break;
                case 9://显示剩余照片总数 
                    _view.UpCtrlRemainPs("还剩未拼照片：" + overPnum.ToString() + " 张");
                    break;
                default:
                    break;
            }
            strResult = "已拼:  " + SusBoardNum.ToString() + " 块大板    已拼照片数： " + TotalPNum.ToString();
            _view.UpCtrl(strResult, strStatus);
           
        }


        public void StartFAutoPB()
        {
            SusBoardNum = 0;
            try
            {
                if (_bll.CurentPrinter==null)
                {
                    _bll.CurentPrinter = DataTransfer.CurPrinter;
                }
                if (thAutoMakeup == null)
                {
                    thAutoMakeup = new Thread(FAutoPB);
                    thAutoMakeup.Start();
                }
                else if (thAutoMakeup.ThreadState == ThreadState.Stopped || thAutoMakeup.ThreadState == ThreadState.Background || thAutoMakeup.ThreadState == ThreadState.WaitSleepJoin || thAutoMakeup.ThreadState == ThreadState.Suspended)
                {
                    thAutoMakeup = new Thread(FAutoPB);
                    thAutoMakeup.Start();
                }
                else
                {
                    MessageBox.Show("无法执行操作，请重试！");
                    _view.CloseFrm();
                }

                _view.SetBtn("暂停",true);
            }
            catch (Exception)
            {

                throw;
            }
        }

        public void StopFAutoPB()
        {
            try
            {
                _bll.IsStop = true;             
                //thAutoMakeup.Join();
                //thAutoMakeup.Abort();
                _view.SetBtn("正在停止拼板",false);

                //_view.UpCtrl("...", "正在停止拼板...");
                //while (!_bll.HasStoped)
                //{
                //    Thread.Sleep(500);
                //}
            }
            catch (ThreadAbortException ex)
            {
                Thread.ResetAbort();
            }
            finally
            {
                //_view.SetBtn("开始");
            }
        }

        public bool CloseConfirm()
        {
            if (thAutoMakeup!=null&&thAutoMakeup.ThreadState == ThreadState.Running)
            {
                DialogResult result = MessageBox.Show("正在自动拼板，是否要停止并退出自动拼版？", "确认", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (result == DialogResult.Yes)
                {
                    StopFAutoPB();
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else
            {
                return true;
            }
        }


        private void FAutoPB()
        {
            float rate = 0;
            int result = 0;
            rate = _view.CtmRaste;
            _bll.IsStop = false;
          //  result = _bll.FAUTOPB(rate, DataTransfer.curTemplate, DataTransfer.curUid, DataTransfer.CurPrinter,DataTransfer.BarCodeStatus);
            //_bll.GetPhotoByPorduct(1, 2, 2);
            if (result == 3)//需要正常结束
            {
                StopFAutoPB();
            }
        }
    }
}
